package com.yoga.admin.api.annotation;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.YogaAdminBO;
import com.yoga.manager.uitl.JsonUtil;
import com.yoga.manager.uitl.RedisKeyConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.ValueOperations;

import java.util.concurrent.TimeUnit;

/**
 * Created by chenjin on 2020/2/27.
 */
public class UserTokenManage {
    private static UserTokenManage userTokenManage;
    private ValueOperations valueOperations;
    private static Cache cacheUserData;


    public static  UserTokenManage getInstance(ValueOperations valueOperations){

        if (cacheUserData==null) {
            cacheUserData = CacheBuilder.newBuilder().concurrencyLevel(10).maximumSize(1000).expireAfterWrite(5, TimeUnit.MINUTES).build();
        }

        if (userTokenManage==null){
            return new UserTokenManage(valueOperations);
        }
       return userTokenManage;
    }

    private UserTokenManage(){}
    private UserTokenManage(ValueOperations valueOperations){
        this.valueOperations=valueOperations;
    }



    public YogaAdminBO getLoginUser(Long yogaAdminId) throws AppException{
        YogaAdminBO yogaAdminBO=null;
        if (yogaAdminId==null){
            return null;
        }
        String key= RedisKeyConfig.getYogaAdminBOKey(yogaAdminId);

        yogaAdminBO=(YogaAdminBO) cacheUserData.getIfPresent(key);

        String str= (String) valueOperations.get(key);
        if (StringUtils.isNotEmpty(str)){
            yogaAdminBO= JsonUtil.parseJson(str,YogaAdminBO.class);
            //重新设置超时时间
            setLoginUser(yogaAdminBO);
            return yogaAdminBO;
        }
        return yogaAdminBO ;
    }

    public void setLoginUser(YogaAdminBO yogaAdminBO) throws AppException{

        if (yogaAdminBO==null){
            return ;
        }
        if (yogaAdminBO.getYogaAdminId()==null){
            throw new AppException(AppErrorCodeEnum.USER_ID_ERROR);
        }
        YogaAdminBO adminBO = new YogaAdminBO();
        adminBO.setYogaAdminId(yogaAdminBO.getYogaAdminId());
        adminBO.setAdminNo(yogaAdminBO.getAdminNo());
        adminBO.setAdminNick(yogaAdminBO.getAdminNick());
        adminBO.setAdminPasswd(yogaAdminBO.getAdminPasswd());
        adminBO.setAdminHeadImg(yogaAdminBO.getAdminHeadImg());
        String key= RedisKeyConfig.getYogaAdminBOKey(adminBO.getYogaAdminId());
        cacheUserData.put(key,adminBO);
        //无操作半小时失效
        valueOperations.set(key,JsonUtil.toJson(adminBO), 30,TimeUnit.MINUTES);
        return  ;
    }


    public void delete(Long yogaAdminId) throws AppException{
        if (yogaAdminId==null){
            throw new AppException(AppErrorCodeEnum.TOCKEN_IS_NULL);
        }
        String key= RedisKeyConfig.getYogaAdminBOKey(yogaAdminId);
        cacheUserData.cleanUp();
        //无操作半小时失效
        valueOperations.set(key,"11", 1,TimeUnit.SECONDS);
        return  ;
    }

}
